#!/bin/bash
########################################################################
# script name:  find_frame_info.sh
# Used for:	getting lpar's information from hmc's
# Author:       Martin Dumont
# Contributors:
# Version:      0.1
#
# (date format: date +'%Y/%m/%d')
#
# Date          Version Contributors    Modifications
# ----------    ------- ------------    -------------
# 2011/01/21	1.0	MDUMONT		kick-off			
# 2011/02/02	1.1	MDUMONT		added default_profile
# 2011/02/15	1.2	MDUMONT		added HMC MHAE770a
# 2011/03/10	1.3	MDUMONT		changed ssh user to sap_viewer and added mhae770a hmc
# 2011/08/11	1.4	MDUMONT		removed mhae04 (firmware update)
# 2011/08/11	1.5	MDUMONT		added de-activate option
# 2011/12/12	1.6	MDUMONT		changed tr from upper/lower to A-Z/a-z
########################################################################
##################################
# OPTIONS and Usage
##################################
usage()
{
cat << EOF

usage: $0 [options] [ARG]

This script gets all LPAR's information from all HMC's
If an action is specified, it won't execute it, only show what to type

OPTIONS:
   -a               All hosts (detail output)
   -s [u|d]         Host status [u]p or [d]own
   -h [hostname]    Specific host to query
   -r [u|d]         LPAR status [u]p or [d]own
   -g               Generate a host file from pingable hosts (like /etc/hosts)
   -L               List HMC's and SYSTEMS
   -l               Specific HMC to query (seperate multiple HMC with comma)
   -c               Show console command
   -A               Activate an LPAR
   -D               De-Activate an LPAR

Ex.:
$0 -a
$0 -s u
$0 -h saprmt
$0 -h saprmt01,saprmt02
$0 -h saprmt01 -g
$0 -L
$0 -l dhae04
$0 -c -h saprmt01
$0 -A -h saprmt01

EOF
}
##################################
# Set default HMC list
##################################
HMCREF="mhae770a"
#HMCREF="mhae770a mhae595a dhae595a dhae04 dhae00"
#HMCREF="mhae770a mhae595a dhae595a mhae04 dhae04 dhae00"
##################################
while getopts as:gLl:r:h:cAD OPTION
do
	case $OPTION in
	a)
		VERBOSE=1
		;;
	s)
		SHOWHOSTSTATE=$(echo $OPTARG | tr '[a-z]' '[A-Z]')
		if [ "$SHOWHOSTSTATE" != "U" ] && [ "$SHOWHOSTSTATE" != "D" ]
		then echo "Please specified a HOST state ([u]p or [d]own)"; exit 1
		elif [ $SHOWHOSTSTATE = U ]; then SHOWHOSTSTATE=UP
		elif [ $SHOWHOSTSTATE = D ]; then SHOWHOSTSTATE=DOWN
		fi
		;;
	g)
		CREATEHOSTFILE=1
		;;
	L)
		LISTHMCS=1
		;;
	l)
		HMCREF=$(echo $OPTARG | sed 's/,/ /g')
		;;
	r)
		LPARSTATE=$(echo $OPTARG | tr '[a-z]' '[A-Z]')
		if [ $LPARSTATE != U ] && [ $LPARSTATE != D ]
		then echo "Please specified a LPAR state"; exit 1
		elif [ $LPARSTATE = U ]; then LPARSTATE="Not-Activated"
		elif [ $LPARSTATE = D ]; then LPARSTATE="Running"
		fi
		;;
	h)
		# no beginning - or . in the name, striping them off, then changing , to | for the "egrep" further below
		LPARNAME=$(echo $OPTARG | sed 's/,/|/g' | sed 's/^-*//' | cut -d. -f1 | tr '[A-Z]' '[a-z]')
		;;
	c)
		SHOWCONSOLE=1
		;;
	A)
		ACTIVATE=1
		;;
	D)
		DEACTIVATE=1
		;;
	\?)
		usage
		exit 1
		;;
	*)
		usage
		exit 1
		;;
	esac
done
if [ $OPTIND = 1 ]; then usage; exit 1; fi
##################################
# Set precedance
##################################
if [ ! -z $VERBOSE ]
then
	unset LPARNAME SHOWCONSOLE CREATEHOSTFILE LISTHMCS
else
	if [ ! -z $LISTHMCS ]; then unset LPARNAME SHOWCONSOLE CREATEHOSTFILE; fi
fi
##################################
# Setting up header
##################################
echo "#----------------------------------------------------------------------------------#"
echo "# Command: $0 $@"
echo "# HMC = ${HMCREF}"
if [ ! -z $LISTHMCS ];then echo "# Output = HMC:SYSTEM";fi 
if [ ! -z $LPARNAME ];then echo "# Server = `echo $LPARNAME | sed 's/|/*,/g' | sed 's/$/*/'`";fi
if [ ! -z $SHOWCONSOLE ];then echo "# Show console command for host(s), escape with [~.]";fi
echo "#----------------------------------------------------------------------------------#"
if [ ! -z $CREATEHOSTFILE ]
then
	echo "# Output = IP of HOSTS"; 
	echo "#---------------------------------------------------------------------------------------------------------------------------------------------------#"
	echo -e "# IP:HOST:DNS:DETAILS:STATUS #" | awk -F':' '{ printf "%-15s%-12s%-37s%-77s%-5s\n", $1, $2, $3, $4, $5 }'
	echo "#---------------------------------------------------------------------------------------------------------------------------------------------------#"
fi
if [ ! -z $VERBOSE ]; then echo "# Output = HMC:SYSTEM:LPAR DETAILS:DNS NAME:SERVER IP:PING STATUS";fi
##################################
# MAIN
##################################
SUMMARYFILE="/tmp/sum_${RANDOM}_`date +'%Y%m%d%H%M'`"
cp /dev/null $SUMMARYFILE
for HMC in $HMCREF
do
	PING=$(ping -c 1 -w 1 $HMC 2> /dev/null)
	RC=$?
	if [[ $RC = 0 ]]
	then
		sudo ssh -x -2 -q sap_viewer@$HMC "for MANAGEDSYS in \`lssyscfg -r sys -F name,state|grep Operating|cut -d, -f1\`;do \
		lssyscfg -r lpar -m \$MANAGEDSYS -F name,lpar_env,state,os_version,default_profile | sed \"s/ /-/g\" \
		| sed \"s/^/\$MANAGEDSYS /g\"; done" \
		| sed "s/^/$HMC /g" | sed 's/ /:/g' | grep -v "^.*:.*:.*,.*,$LPARSTATE,.*$" | egrep -i "$LPARNAME" >> $SUMMARYFILE
	else
		echo "HMC $HMC not pingable";
	fi
done
if [ $(du $SUMMARYFILE | cut -f1) = 0 ]; then echo "####################---> NOTHING FOUND <---######################";exit 1 ; fi
##################################
# Output only HMC and SYSTEM (option -l)
##################################
if [ ! -z $LISTHMCS ]; then cat $SUMMARYFILE | cut -d: -f1,2 | sort | uniq; rm $SUMMARYFILE; exit 0; fi
##################################
# All output production
##################################
cat $SUMMARYFILE | while read LINE
do
	HMC=$(echo $LINE | cut -d":" -f1)
	SYSTEM=$(echo $LINE | cut -d":" -f2)
	LPAR=$(echo $LINE | cut -d":" -f3 | cut -d, -f1)
	SERVER=$(echo $LPAR | sed 's/@//g' | tr '[A-Z]' '[a-z]')
	TYPE=$(echo $LINE | cut -d":" -f3 | cut -d, -f2)
	STATE=$(echo $LINE | cut -d":" -f3 | cut -d, -f3)
	PROFILE=$(echo $LINE | cut -d":" -f3 | cut -d, -f5)
	##################################
	# Verifying if console command need to be echoed
	# Ex.:
	# sudo ssh -x -t -e none sap_viewer@`echo $CONSOLE|cut -d' ' -f1` "mkvterm -m `echo $CONSOLE|cut -d' ' -f2` -p `echo $CONSOLE|cut -d' ' -f3`"
	##################################
	if [ "$SHOWCONSOLE" == "1" ]
	then
		echo sudo ssh -x -t -e none hscroot@$HMC \"mkvterm -m $SYSTEM -p $LPAR\"
	fi
	if [ "$ACTIVATE" == "1" ]
	then
		echo sudo ssh -x -t -e none hscroot@$HMC \"chsysstate -m $SYSTEM -r lpar -n $LPAR -f \'$PROFILE\' -o on\"
	fi
	if [ "$DEACTIVATE" == "1" ]
	then
		echo sudo ssh -x -t -e none hscroot@$HMC \"chsysstate -m $SYSTEM -r lpar -n $LPAR -o shutdown --immed\"
	fi
	##################################
	# Set defaults
	##################################
	IP=NOT_AVAILABLE
	DNS=NOT_AVAILABLE
	PING=NOT_AVAILABLE
	#if [[ $TYPE = aixlinux ]] && [[ $STATE = Running ]]
	#then
		##################################
		# Check PING status
		##################################
		PING=$(ping -c 1 -w 1 $SERVER 2> /dev/null)
		RC=$?; if [[ $RC = 0 ]];then PING=UP; else PING=DOWN; fi
		##################################
		# Get HOST DNS and IP
		##################################
		HOSTOUT=$(host $SERVER 2> /dev/null)
		RC=$?
		if [[ $RC = 0 ]]
		then
			IP=$(echo $HOSTOUT | cut -d' ' -f3 | sed 's/,//')
			DNS=$(echo $HOSTOUT | cut -d' ' -f1)
		fi
	#fi
	##################################
	# Output only hosts and ip's 
	# Also check SHOWCONSOLE
	##################################
	if [ -z $SHOWCONSOLE ] && [ -z $ACTIVATE ]
	then
		if [ ! -z $CREATEHOSTFILE ]
		then
			if [[ $DNS != NOT_AVAILABLE ]]
			then
				HOST=$(echo $DNS | cut -d. -f1)
				echo -e "$IP:$HOST:$DNS:# HMC=$HMC SYSTEM=$SYSTEM LPAR=$LPAR TYPE=$TYPE:$PING" | grep "${SHOWHOSTSTATE}$" | awk -F':' '{ printf "%-15s%-12s%-37s%-77s%-5s\n", $1, $2, $3, $4, $5 }'
			fi
		else
			##################################
			# Output details (option -a)
			##################################
			echo "$LINE:$SERVER:$DNS:$IP:$PING" | grep "${SHOWHOSTSTATE}$"
		fi
	fi
done
rm -f $SUMMARYFILE 2>&1 > /dev/null
exit 0
